package front

import (
	"errors"
	"hft/Database/admin"
	"hft/global"

	"gorm.io/gorm"
)

type Region struct{}

func (*Region) FindRegion(id int) (list []*admin.Region, err error) {

	var city *admin.Region

	err = global.OC_DB.Where("level = ?", "city").First(&city, id).Error

	if err != nil {
		return nil, errors.New("该城市不存在")
	}

	var regions []*admin.Region

	global.OC_DB.Where("p_id = ?", city.ID).Preload("Districts", func(db *gorm.DB) *gorm.DB {
		return db.Preload("Districts")
	}).Find(&regions)

	return regions, nil

}

var RegionService = new(Region)
